package com.clei.netty.encoder;

import com.clei.utils.ByteUtil;
import com.clei.utils.PrintUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * String -> Byte Encoder
 *
 * @author Y
 * @date 2022-01-13
 */
public class StringToByteEncoder extends MessageToByteEncoder<String> {

    @Override
    protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
        try {
            byte[] data = ByteUtil.hex2Bytes(msg);
            out.writeBytes(data);
        } catch (Exception e) {
            PrintUtil.log("十六进制字符串转byte数组出错 ", e);
        }
    }
}
